package com.mj.mes.system.SysMechanics;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Created by Administrator on 2017/8/21.
 */
@Service
@Transactional
@ConfigurationProperties(value = "sysMechanicsService")
public class SysMechanicsService {
    @Autowired
    private SysMechanicsDao sysMechanicsDao;

    Page<SysMechanics> findAll(Pageable pageable) {
        return sysMechanicsDao.findAll(pageable);
    }

    Page<SysMechanics> findByDeviceName(String s_deviceName, Pageable pageable) {
        Specification<SysMechanics> specification = new Specification<SysMechanics>() {
            @Override
            public Predicate toPredicate(Root<SysMechanics> root,
                                         CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate _name = criteriaBuilder.like(root.get("deviceName"), "%" + s_deviceName + "%");
                return criteriaBuilder.and(_name);
            }
        };
        return sysMechanicsDao.findAll(specification, pageable);
    }

    List<SysMechanics> findAll() {
        return sysMechanicsDao.findAll();
    }

    public SysMechanics findByDevicePosition(String devicePosition){
        return sysMechanicsDao.findByDevicePosition(devicePosition);
    }


    /**
     * 删除
     *
     * @param list
     * @return
     */
    boolean delete(List<SysMechanics> list) {
        boolean flag = false;
        try {
            sysMechanicsDao.deleteInBatch(list);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
//    public Page<SysMechanics> findBySysDeviceName(final String deviceName, int page, Integer size) {
//        Pageable pageable = new PageRequest(page, size, new Sort("id"));
//        Specification<SysMechanics> spec = new Specification<SysMechanics>() {
//
//            public Predicate toPredicate(Root<SysMechanics> root,
//                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
//                return query.where(cb.lt(root.get("id").as(Integer.class), id))
//                        .getRestriction();
//            }
//        };
//        return studentDao.findAll(spec, pageable);
//    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public SysMechanics findOne(Long id) {
        return sysMechanicsDao.findOne(id);
    }

    /**
     * 更新用户
     *
     * @param sysMechanics
     */
    public void updateMechanics(SysMechanics sysMechanics) {
        sysMechanicsDao.save(sysMechanics);
    }

    public void addMechanics(SysMechanics sysMechanics) {
        sysMechanicsDao.save(sysMechanics);
    }
}


